package com.pancm.test.aliyun;


import com.aliyun.ocr20191230.Client;
import com.aliyun.ocr20191230.models.*;
import com.aliyun.tea.TeaException;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;
 
import java.io.*;
 
/**
 * @Author: 四块五毛六
 * @Date: 2022/7/19 14:49
 */
public class SampleOCR {
    private final static String AccessKeyID = "LTAI5t7LdJjQ5yrRc9dTwAUM";
    private final static String AccessKeySecret = "OiqBipld6Y5VSdfC5L2xi1My8hI0Em";
    public static void main(String[] args_) throws Exception {
        Config config = new Config();
        //你的accessKeyId
        config.accessKeyId = AccessKeyID;
        //你的accessKeySecret
        config.accessKeySecret = AccessKeySecret;
        config.type = "access_key";
        config.regionId = "cn-shanghai";
        // config.endpointType="internal";  //默认通过公网访问OSS，如需通过内网请打开这一行
        Client client = new Client(config);
        RuntimeOptions runtimeOptions = new RuntimeOptions();
//        FileUtils fileUtils = FileUtils.getInstance("你的accessKeyId", "你的accessKeySecret");
//        //这个是通过本地上传的方式，然后如果是移动端的话，他传过来的就应该是一个file文件，直接用file.getInputStream就行
//        InputStream in = new FileInputStream(new File("C:\\Users\\Chongchong\\Desktop\\23.jpg"));
//        //这个前面的fileName是生成后的ossUrl的后缀，其实有没有都无所谓，或者你自己取一个，如果加上.jpg等图片后缀，
//        //访问这个链接的时候就是下载一张本地图片，如果不加的话，下载的就是一个未识别的文件，如果改后缀可以变成图片
//        String ossUrl = fileUtils.upload("", in);
        //url方式
        String ossUrl = "https://viapi-test-bj.oss-cn-beijing.aliyuncs.com/viapi-3.0domepic/ocr/RecognizeIdentityCard/sfzbm1.jpg";
        //身份证
        recognizeIdCardAdvance(client, ossUrl);
 
        //驾驶证
//        recognizeDriverLicense(client, ossUrl);
 
        //行驶证
//        recognizeDrivingLicense(client, ossUrl);
 
 
        config.endpoint = "ocr.cn-shanghai.aliyuncs.com";
 
    }
 
    //行驶证（就写了这一个，因为其他的都跟这个一样，只有方法不一样）
    private static void recognizeDrivingLicense(Client client, String ossUrl) {
        //face是正面，back是背面，我们一般用正面就行，这个可以传过来的
        RecognizeDrivingLicenseRequest request = new RecognizeDrivingLicenseRequest()
                .setImageURL(ossUrl)
                .setSide("face");
        try {
            // 复制代码运行请自行打印 API 的返回值
            RecognizeDrivingLicenseResponse recognizeDrivingLicenseResponse = client.recognizeDrivingLicense(request);
            //这个打印的就是recognizeDrivingLicenseResponse就是内容，最好用断点打，这是整个的内容，打印不出来东西的，
            //需要你自己从中间解析，解析我就不写了，因为我也没有解析
            System.out.println(recognizeDrivingLicenseResponse);
        } catch (TeaException error) {
            // 如有需要，请打印 error
            com.aliyun.teautil.Common.assertAsString(error.message);
            System.out.println(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 如有需要，请打印 error
            com.aliyun.teautil.Common.assertAsString(error.message);
            System.out.println(error.message);
        }
    }
 
    //驾驶证
    private static void recognizeDriverLicense(Client client, String ossUrl) {
        RecognizeDriverLicenseRequest request = new RecognizeDriverLicenseRequest()
                .setImageURL(ossUrl)
                .setSide("face");
        try {
            // 复制代码运行请自行打印 API 的返回值
            RecognizeDriverLicenseResponse recognizeDriverLicenseResponse = client.recognizeDriverLicense(request);
            System.out.println(recognizeDriverLicenseResponse);
        } catch (TeaException error) {
            // 如有需要，请打印 error
            com.aliyun.teautil.Common.assertAsString(error.message);
            System.out.println(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 如有需要，请打印 error
            com.aliyun.teautil.Common.assertAsString(error.message);
            System.out.println(error.message);
        }
    }
 
    //身份证
    private static void recognizeIdCardAdvance(Client client, String ossUrl) throws Exception {
        RecognizeIdentityCardRequest recognizeIdentityCardRequest = new RecognizeIdentityCardRequest()
                .setImageURL(ossUrl)
                .setSide("back");
//                .setSide("face");
        RuntimeOptions runtime = new RuntimeOptions();
        try {
            // 复制代码运行请自行打印 API 的返回值
            RecognizeIdentityCardResponse recognizeIdentityCardResponse = client.recognizeIdentityCardWithOptions(recognizeIdentityCardRequest, runtime);
 
            System.out.println(recognizeIdentityCardResponse);
        } catch (TeaException error) {
            // 如有需要，请打印 error
            com.aliyun.teautil.Common.assertAsString(error.message);
            System.out.println(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 如有需要，请打印 error
            com.aliyun.teautil.Common.assertAsString(error.message);
            System.out.println(error.message);
        }
    }
}